﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.ComponentModel;

using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;

using TTServer.Messages;

namespace TTServer
{
    public class BluetoothServer
    {
        //unique service identifier
        //public static Guid ServiceGUID = new Guid("ABF32797-4DAE-4890-A23D-33DC8E3E2111");
        public static Guid ServiceGUID = new Guid("300ad0a7-059d-4d97-b9a3-eabe5f6af813");
        
        private BluetoothRadio br;
        private BluetoothListener bl;
        private Thread thread;

        private BluetoothServerThread st = null;

        private bool _NotSupported = true;
        public bool NotSupported
        {
            get
            {
                return _NotSupported;
            }
            set
            {
                _NotSupported = value;
            }
        }

        private bool _Listening = false;
        public bool Listening
        {
            get
            {
                return _Listening;
            }
            set
            {
                _Listening = value;
            }
        }

        public BluetoothServer()
        {
            Listening = false;

            br = BluetoothRadio.PrimaryRadio;
            if (br == null)
            {
                NotSupported = true;
            }
            else
            {
                NotSupported = false;
            }
        }

        public void StartBluetooth()
        {
            Listening = true;
            try
            {
                bl = new BluetoothListener(ServiceGUID);
                bl.Start();
            }
            catch
            {
                NotSupported = true;
                return;
            }

            thread = new Thread(new ThreadStart(ListenLoop));
            thread.Name = "Bluetooth Server";
            thread.Start();
        }

        public void StopBluetooth()
        {
            if (st != null)
            {
                st.TerminateAll();
            }

            if (bl != null)
            {
                Listening = false;

                try
                {
                    bl.Stop();
                }
                catch
                { 
                
                }

                //thread.Abort();
            }
        }

        private BluetoothClient bc;

        private void ListenLoop()
        {
            while (Listening)
            {
                
                try
                {
                    bc = bl.AcceptBluetoothClient();

                    st = new BluetoothServerThread(bc);
                    st.CreateAndStartThread();

                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                    //break;
                }
            }
        }
    }
}
